home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / sendmail.8.8.4.tar.gz / sendmail.8.8.4.tar / sendmail-8.8.4 / mail.local / mail.local.c < prev    next >
C/C++ Source or Header  |  1996-11-24  |  21KB  |  923 lines

  1. /*-
  2.  * Copyright (c) 1990, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char copyright[] =
  36. "@(#) Copyright (c) 1990, 1993, 1994\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)mail.local.c    8.34 (Berkeley) 11/24/96";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * This is not intended to compile on System V derived systems
  46.  * such as Solaris or HP-UX, since they use a totally different
  47.  * approach to mailboxes (essentially, they have a setgid program
  48.  * rather than setuid, and they rely on the ability to "give away"
  49.  * files to do their work).  IT IS NOT A BUG that this doesn't
  50.  * compile on such architectures.
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <sys/stat.h>
  55. #include <sys/socket.h>
  56.  
  57. #include <netinet/in.h>
  58.  
  59. #include <errno.h>
  60. #include <fcntl.h>
  61. #include <netdb.h>
  62. #include <pwd.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <syslog.h>
  67. #include <time.h>
  68. #include <unistd.h>
  69. #ifdef EX_OK
  70. # undef EX_OK        /* unistd.h may have another use for this */
  71. #endif
  72. #include <sysexits.h>
  73. #include <ctype.h>
  74.  
  75. #if __STDC__
  76. #include <stdarg.h>
  77. #else
  78. #include <varargs.h>
  79. #endif
  80.  
  81. #if (defined(sun) && defined(__svr4__)) || defined(__SVR4)
  82. # define USE_LOCKF    1
  83. # define USE_SETEUID    1
  84. # define _PATH_MAILDIR    "/var/mail"
  85. #endif
  86.  
  87. #if defined(_AIX)
  88. # define USE_LOCKF    1
  89. # define USE_VSYSLOG    0
  90. #endif
  91.  
  92. #if defined(ultrix)
  93. # define USE_VSYSLOG    0
  94. #endif
  95.  
  96. #if defined(__osf__)
  97. # define USE_VSYSLOG    0
  98. #endif
  99.  
  100. #if defined(NeXT)
  101. # include <libc.h>
  102. # define _PATH_MAILDIR    "/usr/spool/mail"
  103. # define __dead        /* empty */
  104. # define S_IRUSR    S_IREAD
  105. # define S_IWUSR    S_IWRITE
  106. #endif
  107.  
  108. /*
  109.  * If you don't have flock, you could try using lockf instead.
  110.  */
  111.  
  112. #ifdef USE_LOCKF
  113. # define flock(a, b)    lockf(a, b, 0)
  114. # define LOCK_EX    F_LOCK
  115. #endif
  116.  
  117. #ifndef USE_VSYSLOG
  118. # define USE_VSYSLOG    1
  119. #endif
  120.  
  121. #ifndef LOCK_EX
  122. # include <sys/file.h>
  123. #endif
  124.  
  125. #ifdef BSD4_4
  126. # include "pathnames.h"
  127. #endif
  128.  
  129. #ifndef __P
  130. # ifdef __STDC__
  131. #  define __P(protos)    protos
  132. # else
  133. #  define __P(protos)    ()
  134. #  define const
  135. # endif
  136. #endif
  137. #ifndef __dead
  138. # if defined(__GNUC__) && (__GNUC__ < 2 || __GNUC_MINOR__ < 5) && !defined(__STRICT_ANSI__)
  139. #  define __dead    __volatile
  140. # else
  141. #  define __dead
  142. # endif
  143. #endif
  144.  
  145. #ifndef BSD4_4
  146. # define _BSD_VA_LIST_    va_list
  147. #endif
  148.  
  149. #if !defined(BSD4_4) && !defined(linux)
  150. extern char    *strerror __P((int));
  151. extern int    snprintf __P((char *, int, const char *, ...));
  152. extern FILE    *fdopen __P((int, const char *));
  153. #endif
  154.  
  155. /*
  156.  * If you don't have setreuid, and you have saved uids, and you have
  157.  * a seteuid() call that doesn't try to emulate using setuid(), then
  158.  * you can try defining USE_SETEUID.
  159.  */
  160. #ifdef USE_SETEUID
  161. # define setreuid(r, e)        seteuid(e)
  162. #endif
  163.  
  164. #ifndef _PATH_LOCTMP
  165. # define _PATH_LOCTMP    "/tmp/local.XXXXXX"
  166. #endif
  167. #ifndef _PATH_MAILDIR
  168. # define _PATH_MAILDIR    "/var/spool/mail"
  169. #endif
  170.  
  171. #ifndef S_ISREG
  172. # define S_ISREG(mode)    (((mode) & _S_IFMT) == S_IFREG)
  173. #endif
  174.  
  175. int eval = EX_OK;            /* sysexits.h error value. */
  176.  
  177. void        deliver __P((int, char *));
  178. void        e_to_sys __P((int));
  179. __dead void    err __P((const char *, ...));
  180. void        notifybiff __P((char *));
  181. int        store __P((char *));
  182. void        usage __P((void));
  183. void        vwarn __P((const char *, _BSD_VA_LIST_));
  184. void        warn __P((const char *, ...));
  185. void        lockmbox __P((char *));
  186. void        unlockmbox __P((void));
  187.  
  188. int
  189. main(argc, argv)
  190.     int argc;
  191.     char *argv[];
  192. {
  193.     struct passwd *pw;
  194.     int ch, fd;
  195.     uid_t uid;
  196.     char *from;
  197.     extern char *optarg;
  198.     extern int optind;
  199.  
  200.     /* make sure we have some open file descriptors */
  201.     for (fd = 10; fd < 30; fd++)
  202.         (void) close(fd);
  203.  
  204.     /* use a reasonable umask */
  205.     (void) umask(0077);
  206.  
  207. #ifdef LOG_MAIL
  208.     openlog("mail.local", 0, LOG_MAIL);
  209. #else
  210.     openlog("mail.local", 0);
  211. #endif
  212.  
  213.     from = NULL;
  214.     while ((ch = getopt(argc, argv, "df:r:")) != EOF)
  215.         switch(ch) {
  216.         case 'd':        /* Backward compatible. */
  217.             break;
  218.         case 'f':
  219.         case 'r':        /* Backward compatible. */
  220.             if (from != NULL) {
  221.                 warn("multiple -f options");
  222.                 usage();
  223.             }
  224.             from = optarg;
  225.             break;
  226.         case '?':
  227.         default:
  228.             usage();
  229.         }
  230.     argc -= optind;
  231.     argv += optind;
  232.  
  233.     if (!*argv)
  234.         usage();
  235.  
  236.     /*
  237.      * If from not specified, use the name from getlogin() if the
  238.      * uid matches, otherwise, use the name from the password file
  239.      * corresponding to the uid.
  240.      */
  241.     uid = getuid();
  242.     if (!from && (!(from = getlogin()) ||
  243.         !(pw = getpwnam(from)) || pw->pw_uid != uid))
  244.         from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
  245.  
  246.     /*
  247.      * There is no way to distinguish the error status of one delivery
  248.      * from the rest of the deliveries.  So, if we failed hard on one
  249.      * or more deliveries, but had no failures on any of the others, we
  250.      * return a hard failure.  If we failed temporarily on one or more
  251.      * deliveries, we return a temporary failure regardless of the other
  252.      * failures.  This results in the delivery being reattempted later
  253.      * at the expense of repeated failures and multiple deliveries.
  254.      */
  255.     for (fd = store(from); *argv; ++argv)
  256.         deliver(fd, *argv);
  257.     exit(eval);
  258. }
  259.  
  260. int
  261. store(from)
  262.     char *from;
  263. {
  264.     FILE *fp;
  265.     time_t tval;
  266.     int fd, eline;
  267.     char line[2048];
  268.     char tmpbuf[sizeof _PATH_LOCTMP + 1];
  269.  
  270.     strcpy(tmpbuf, _PATH_LOCTMP);
  271.     if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) {
  272.         e_to_sys(errno);
  273.         err("unable to open temporary file");
  274.     }
  275.     (void)unlink(tmpbuf);
  276.  
  277.     (void)time(&tval);
  278.     (void)fprintf(fp, "From %s %s", from, ctime(&tval));
  279.  
  280.     line[0] = '\0';
  281.     for (eline = 1; fgets(line, sizeof(line), stdin);) {
  282.         if (line[0] == '\n')
  283.             eline = 1;
  284.         else {
  285.             if (eline && line[0] == 'F' &&
  286.                 !memcmp(line, "From ", 5))
  287.                 (void)putc('>', fp);
  288.             eline = 0;
  289.         }
  290.         (void)fprintf(fp, "%s", line);
  291.         if (ferror(fp)) {
  292.             e_to_sys(errno);
  293.             err("temporary file write error");
  294.         }
  295.     }
  296.  
  297.     /* If message not newline terminated, need an extra. */
  298.     if (!strchr(line, '\n'))
  299.         (void)putc('\n', fp);
  300.     /* Output a newline; note, empty messages are allowed. */
  301.     (void)putc('\n', fp);
  302.  
  303.     if (fflush(fp) == EOF || ferror(fp)) {
  304.         e_to_sys(errno);
  305.         err("temporary file write error");
  306.     }
  307.     return (fd);
  308. }
  309.  
  310. void
  311. deliver(fd, name)
  312.     int fd;
  313.     char *name;
  314. {
  315.     struct stat fsb, sb;
  316.     struct passwd *pw;
  317.     int mbfd, nr, nw, off;
  318.     char *p;
  319.     char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
  320.     off_t curoff;
  321.  
  322.     /*
  323.      * Disallow delivery to unknown names -- special mailboxes can be
  324.      * handled in the sendmail aliases file.
  325.      */
  326.     if (!(pw = getpwnam(name))) {
  327.         if (eval != EX_TEMPFAIL)
  328.             eval = EX_UNAVAILABLE;
  329.         warn("unknown name: %s", name);
  330.         return;
  331.     }
  332.     endpwent();
  333.  
  334.     /*
  335.      * Keep name reasonably short to avoid buffer overruns.
  336.      *    This isn't necessary on BSD because of the proper
  337.      *    definition of snprintf(), but it can cause problems
  338.      *    on other systems.
  339.      * Also, clear out any bogus characters.
  340.      */
  341.  
  342.     if (strlen(name) > 40)
  343.         name[40] = '\0';
  344.     for (p = name; *p != '\0'; p++)
  345.     {
  346.         if (!isascii(*p))
  347.             *p &= 0x7f;
  348.         else if (!isprint(*p))
  349.             *p = '.';
  350.     }
  351.  
  352.     (void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name);
  353.  
  354.     /*
  355.      * If the mailbox is linked or a symlink, fail.  There's an obvious
  356.      * race here, that the file was replaced with a symbolic link after
  357.      * the lstat returned, but before the open.  We attempt to detect
  358.      * this by comparing the original stat information and information
  359.      * returned by an fstat of the file descriptor returned by the open.
  360.      *
  361.      * NB: this is a symptom of a larger problem, that the mail spooling
  362.      * directory is writeable by the wrong users.  If that directory is
  363.      * writeable, system security is compromised for other reasons, and
  364.      * it cannot be fixed here.
  365.      *
  366.      * If we created the mailbox, set the owner/group.  If that fails,
  367.      * just return.  Another process may have already opened it, so we
  368.      * can't unlink it.  Historically, binmail set the owner/group at
  369.      * each mail delivery.  We no longer do this, assuming that if the
  370.      * ownership or permissions were changed there was a reason.
  371.      *
  372.      * XXX
  373.      * open(2) should support flock'ing the file.
  374.      */
  375. tryagain:
  376.     lockmbox(path);
  377.     if (lstat(path, &sb)) {
  378.         mbfd = open(path,
  379.             O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
  380.         if (mbfd == -1) {
  381.             if (errno == EEXIST)
  382.                 goto tryagain;
  383.         } else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) {
  384.             e_to_sys(errno);
  385.             warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name);
  386.             goto err1;
  387.         }
  388.     } else if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) {
  389.         e_to_sys(errno);
  390.         warn("%s: irregular file", path);
  391.         goto err0;
  392.     } else if (sb.st_uid != pw->pw_uid) {
  393.         eval = EX_CANTCREAT;
  394.         warn("%s: wrong ownership (%d)", path, sb.st_uid);
  395.         goto err0;
  396.     } else {
  397.         mbfd = open(path, O_APPEND|O_WRONLY, 0);
  398.         if (mbfd != -1 &&
  399.             (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
  400.             !S_ISREG(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
  401.             sb.st_ino != fsb.st_ino || sb.st_uid != fsb.st_uid)) {
  402.             eval = EX_CANTCREAT;
  403.             warn("%s: file changed after open", path);
  404.             goto err1;
  405.         }
  406.     }
  407.  
  408.     if (mbfd == -1) {
  409.         e_to_sys(errno);
  410.         warn("%s: %s", path, strerror(errno));
  411.         goto err0;
  412.     }
  413.  
  414.     /* Wait until we can get a lock on the file. */
  415.     if (flock(mbfd, LOCK_EX)) {
  416.         e_to_sys(errno);
  417.         warn("%s: %s", path, strerror(errno));
  418.         goto err1;
  419.     }
  420.  
  421.     /* Get the starting offset of the new message for biff. */
  422.     curoff = lseek(mbfd, (off_t)0, SEEK_END);
  423.     (void)snprintf(biffmsg, sizeof(biffmsg),
  424.         sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n", 
  425.         name, curoff);
  426.  
  427.     /* Copy the message into the file. */
  428.     if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  429.         e_to_sys(errno);
  430.         warn("temporary file: %s", strerror(errno));
  431.         goto err1;
  432.     }
  433.     if (setreuid(0, pw->pw_uid) < 0) {
  434.         e_to_sys(errno);
  435.         warn("setreuid(0, %d): %s (r=%d, e=%d)",
  436.              pw->pw_uid, strerror(errno), getuid(), geteuid());
  437.         goto err1;
  438.     }
  439. #ifdef DEBUG
  440.     printf("new euid = %d\n", geteuid());
  441. #endif
  442.     while ((nr = read(fd, buf, sizeof(buf))) > 0)
  443.         for (off = 0; off < nr; off += nw)
  444.             if ((nw = write(mbfd, buf + off, nr - off)) < 0) {
  445.                 e_to_sys(errno);
  446.                 warn("%s: %s", path, strerror(errno));
  447.                 goto err3;
  448.             }
  449.     if (nr < 0) {
  450.         e_to_sys(errno);
  451.         warn("temporary file: %s", strerror(errno));
  452.         goto err3;
  453.     }
  454.  
  455.     /* Flush to disk, don't wait for update. */
  456.     if (fsync(mbfd)) {
  457.         e_to_sys(errno);
  458.         warn("%s: %s", path, strerror(errno));
  459. err3:
  460.         if (setreuid(0, 0) < 0) {
  461.             e_to_sys(errno);
  462.             warn("setreuid(0, 0): %s", strerror(errno));
  463.         }
  464. #ifdef DEBUG
  465.         printf("reset euid = %d\n", geteuid());
  466. #endif
  467. err2:        (void)ftruncate(mbfd, curoff);
  468. err1:        (void)close(mbfd);
  469. err0:        unlockmbox();
  470.         return;
  471.     }
  472.         
  473.     /* Close and check -- NFS doesn't write until the close. */
  474.     if (close(mbfd)) {
  475.         e_to_sys(errno);
  476.         warn("%s: %s", path, strerror(errno));
  477.         unlockmbox();
  478.         return;
  479.     }
  480.  
  481.     if (setreuid(0, 0) < 0) {
  482.         e_to_sys(errno);
  483.         warn("setreuid(0, 0): %s", strerror(errno));
  484.     }
  485. #ifdef DEBUG
  486.     printf("reset euid = %d\n", geteuid());
  487. #endif
  488.     unlockmbox();
  489.     notifybiff(biffmsg);
  490. }
  491.  
  492. /*
  493.  * user.lock files are necessary for compatibility with other
  494.  * systems, e.g., when the mail spool file is NFS exported.
  495.  * Alas, mailbox locking is more than just a local matter.
  496.  * EPA 11/94.
  497.  */
  498.  
  499. char    lockname[MAXPATHLEN];
  500. int    locked = 0;
  501.  
  502. void
  503. lockmbox(path)
  504.     char *path;
  505. {
  506.     int statfailed = 0;
  507.  
  508.     if (locked)
  509.         return;
  510.     sprintf(lockname, "%s.lock", path);
  511.     for (;; sleep(5)) {
  512.         int fd;
  513.         struct stat st;
  514.         time_t now;
  515.  
  516.         fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0);
  517.         if (fd >= 0) {
  518.             locked = 1;
  519.             close(fd);
  520.             return;
  521.         }
  522.         if (stat(lockname, &st) < 0) {
  523.             if (statfailed++ > 5)
  524.                 return;
  525.             continue;
  526.         }
  527.         statfailed = 0;
  528.         time(&now);
  529.         if (now < st.st_ctime + 300)
  530.             continue;
  531.         unlink(lockname);
  532.     }
  533. }
  534.  
  535. void
  536. unlockmbox()
  537. {
  538.     if (!locked)
  539.         return;
  540.     unlink(lockname);
  541.     locked = 0;
  542. }
  543.  
  544. void
  545. notifybiff(msg)
  546.     char *msg;
  547. {
  548.     static struct sockaddr_in addr;
  549.     static int f = -1;
  550.     struct hostent *hp;
  551.     struct servent *sp;
  552.     int len;
  553.  
  554.     if (!addr.sin_family) {
  555.         /* Be silent if biff service not available. */
  556.         if (!(sp = getservbyname("biff", "udp")))
  557.             return;
  558.         if (!(hp = gethostbyname("localhost"))) {
  559.             warn("localhost: %s", strerror(errno));
  560.             return;
  561.         }
  562.         addr.sin_family = hp->h_addrtype;
  563.         memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
  564.         addr.sin_port = sp->s_port;
  565.     }
  566.     if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  567.         warn("socket: %s", strerror(errno));
  568.         return;
  569.     }
  570.     len = strlen(msg) + 1;
  571.     if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
  572.         != len)
  573.         warn("sendto biff: %s", strerror(errno));
  574. }
  575.  
  576. void
  577. usage()
  578. {
  579.     eval = EX_USAGE;
  580.     err("usage: mail.local [-f from] user ...");
  581. }
  582.  
  583. #if __STDC__
  584. void
  585. err(const char *fmt, ...)
  586. #else
  587. void
  588. err(fmt, va_alist)
  589.     const char *fmt;
  590.     va_dcl
  591. #endif
  592. {
  593.     va_list ap;
  594.  
  595. #if __STDC__
  596.     va_start(ap, fmt);
  597. #else
  598.     va_start(ap);
  599. #endif
  600.     vwarn(fmt, ap);
  601.     va_end(ap);
  602.  
  603.     exit(eval);
  604. }
  605.  
  606. void
  607. #if __STDC__
  608. warn(const char *fmt, ...)
  609. #else
  610. warn(fmt, va_alist)
  611.     const char *fmt;
  612.     va_dcl
  613. #endif
  614. {
  615.     va_list ap;
  616.  
  617. #if __STDC__
  618.     va_start(ap, fmt);
  619. #else
  620.     va_start(ap);
  621. #endif
  622.     vwarn(fmt, ap);
  623.     va_end(ap);
  624. }
  625.  
  626. void
  627. vwarn(fmt, ap)
  628.     const char *fmt;
  629.     _BSD_VA_LIST_ ap;
  630. {
  631.     /*
  632.      * Log the message to stderr.
  633.      *
  634.      * Don't use LOG_PERROR as an openlog() flag to do this,
  635.      * it's not portable enough.
  636.      */
  637.     if (eval != EX_USAGE)
  638.         (void)fprintf(stderr, "mail.local: ");
  639.     (void)vfprintf(stderr, fmt, ap);
  640.     (void)fprintf(stderr, "\n");
  641.  
  642. #if USE_VSYSLOG
  643.     /* Log the message to syslog. */
  644.     vsyslog(LOG_ERR, fmt, ap);
  645. #else
  646.     {
  647.         char fmtbuf[10240];
  648.  
  649.         (void) vsprintf(fmtbuf, fmt, ap);
  650.         syslog(LOG_ERR, "%s", fmtbuf);
  651.     }
  652. #endif
  653. }
  654.  
  655. /*
  656.  * e_to_sys --
  657.  *    Guess which errno's are temporary.  Gag me.
  658.  */
  659. void
  660. e_to_sys(num)
  661.     int num;
  662. {
  663.     /* Temporary failures override hard errors. */
  664.     if (eval == EX_TEMPFAIL)
  665.         return;
  666.  
  667.     switch(num) {        /* Hopefully temporary errors. */
  668. #ifdef EAGAIN
  669.     case EAGAIN:        /* Resource temporarily unavailable */
  670. #endif
  671. #ifdef EDQUOT
  672.     case EDQUOT:        /* Disc quota exceeded */
  673. #endif
  674. #ifdef EBUSY
  675.     case EBUSY:        /* Device busy */
  676. #endif
  677. #ifdef EPROCLIM
  678.     case EPROCLIM:        /* Too many processes */
  679. #endif
  680. #ifdef EUSERS
  681.     case EUSERS:        /* Too many users */
  682. #endif
  683. #ifdef ECONNABORTED
  684.     case ECONNABORTED:    /* Software caused connection abort */
  685. #endif
  686. #ifdef ECONNREFUSED
  687.     case ECONNREFUSED:    /* Connection refused */
  688. #endif
  689. #ifdef ECONNRESET
  690.     case ECONNRESET:    /* Connection reset by peer */
  691. #endif
  692. #ifdef EDEADLK
  693.     case EDEADLK:        /* Resource deadlock avoided */
  694. #endif
  695. #ifdef EFBIG
  696.     case EFBIG:        /* File too large */
  697. #endif
  698. #ifdef EHOSTDOWN
  699.     case EHOSTDOWN:        /* Host is down */
  700. #endif
  701. #ifdef EHOSTUNREACH
  702.     case EHOSTUNREACH:    /* No route to host */
  703. #endif
  704. #ifdef EMFILE
  705.     case EMFILE:        /* Too many open files */
  706. #endif
  707. #ifdef ENETDOWN
  708.     case ENETDOWN:        /* Network is down */
  709. #endif
  710. #ifdef ENETRESET
  711.     case ENETRESET:        /* Network dropped connection on reset */
  712. #endif
  713. #ifdef ENETUNREACH
  714.     case ENETUNREACH:    /* Network is unreachable */
  715. #endif
  716. #ifdef ENFILE
  717.     case ENFILE:        /* Too many open files in system */
  718. #endif
  719. #ifdef ENOBUFS
  720.     case ENOBUFS:        /* No buffer space available */
  721. #endif
  722. #ifdef ENOMEM
  723.     case ENOMEM:        /* Cannot allocate memory */
  724. #endif
  725. #ifdef ENOSPC
  726.     case ENOSPC:        /* No space left on device */
  727. #endif
  728. #ifdef EROFS
  729.     case EROFS:        /* Read-only file system */
  730. #endif
  731. #ifdef ESTALE
  732.     case ESTALE:        /* Stale NFS file handle */
  733. #endif
  734. #ifdef ETIMEDOUT
  735.     case ETIMEDOUT:        /* Connection timed out */
  736. #endif
  737. #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
  738.     case EWOULDBLOCK:    /* Operation would block. */
  739. #endif
  740.         eval = EX_TEMPFAIL;
  741.         break;
  742.     default:
  743.         eval = EX_UNAVAILABLE;
  744.         break;
  745.     }
  746. }
  747.  
  748. #if !defined(BSD4_4) && !defined(__osf__)
  749.  
  750. char *
  751. strerror(eno)
  752.     int eno;
  753. {
  754.     extern int sys_nerr;
  755.     extern char *sys_errlist[];
  756.     static char ebuf[60];
  757.  
  758.     if (eno >= 0 && eno <= sys_nerr)
  759.         return sys_errlist[eno];
  760.     (void) sprintf(ebuf, "Error %d", eno);
  761.     return ebuf;
  762. }
  763.  
  764. # endif
  765.  
  766. #if !defined(BSD4_4) && !defined(linux)
  767.  
  768. # if __STDC__
  769. snprintf(char *buf, int bufsiz, const char *fmt, ...)
  770. # else
  771. snprintf(buf, bufsiz, fmt, va_alist)
  772.     char *buf;
  773.     int bufsiz;
  774.     const char *fmt;
  775.     va_dcl
  776. # endif
  777. {
  778.     va_list ap;
  779.  
  780. # if __STDC__
  781.     va_start(ap, fmt);
  782. # else
  783.     va_start(ap);
  784. # endif
  785.     vsprintf(buf, fmt, ap);
  786.     va_end(ap);
  787. }
  788.  
  789. #endif
  790.  
  791. #ifdef ultrix
  792.  
  793. /*
  794.  * Copyright (c) 1987, 1993
  795.  *    The Regents of the University of California.  All rights reserved.
  796.  *
  797.  * Redistribution and use in source and binary forms, with or without
  798.  * modification, are permitted provided that the following conditions
  799.  * are met:
  800.  * 1. Redistributions of source code must retain the above copyright
  801.  *    notice, this list of conditions and the following disclaimer.
  802.  * 2. Redistributions in binary form must reproduce the above copyright
  803.  *    notice, this list of conditions and the following disclaimer in the
  804.  *    documentation and/or other materials provided with the distribution.
  805.  * 3. All advertising materials mentioning features or use of this software
  806.  *    must display the following acknowledgement:
  807.  *    This product includes software developed by the University of
  808.  *    California, Berkeley and its contributors.
  809.  * 4. Neither the name of the University nor the names of its contributors
  810.  *    may be used to endorse or promote products derived from this software
  811.  *    without specific prior written permission.
  812.  *
  813.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  814.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  815.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  816.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  817.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  818.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  819.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  820.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  821.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  822.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  823.  * SUCH DAMAGE.
  824.  */
  825.  
  826. #if defined(LIBC_SCCS) && !defined(lint)
  827. static char sccsid[] = "@(#)mktemp.c    8.1 (Berkeley) 6/4/93";
  828. #endif /* LIBC_SCCS and not lint */
  829.  
  830. #include <sys/types.h>
  831. #include <sys/stat.h>
  832. #include <fcntl.h>
  833. #include <errno.h>
  834. #include <stdio.h>
  835. #include <ctype.h>
  836.  
  837. static int _gettemp();
  838.  
  839. mkstemp(path)
  840.     char *path;
  841. {
  842.     int fd;
  843.  
  844.     return (_gettemp(path, &fd) ? fd : -1);
  845. }
  846.  
  847. /*
  848. char *
  849. mktemp(path)
  850.     char *path;
  851. {
  852.     return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
  853. }
  854. */
  855.  
  856. static
  857. _gettemp(path, doopen)
  858.     char *path;
  859.     register int *doopen;
  860. {
  861.     extern int errno;
  862.     register char *start, *trv;
  863.     struct stat sbuf;
  864.     u_int pid;
  865.  
  866.     pid = getpid();
  867.     for (trv = path; *trv; ++trv);        /* extra X's get set to 0's */
  868.     while (*--trv == 'X') {
  869.         *trv = (pid % 10) + '0';
  870.         pid /= 10;
  871.     }
  872.  
  873.     /*
  874.      * check the target directory; if you have six X's and it
  875.      * doesn't exist this runs for a *very* long time.
  876.      */
  877.     for (start = trv + 1;; --trv) {
  878.         if (trv <= path)
  879.             break;
  880.         if (*trv == '/') {
  881.             *trv = '\0';
  882.             if (stat(path, &sbuf))
  883.                 return(0);
  884.             if (!S_ISDIR(sbuf.st_mode)) {
  885.                 errno = ENOTDIR;
  886.                 return(0);
  887.             }
  888.             *trv = '/';
  889.             break;
  890.         }
  891.     }
  892.  
  893.     for (;;) {
  894.         if (doopen) {
  895.             if ((*doopen =
  896.                 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
  897.                 return(1);
  898.             if (errno != EEXIST)
  899.                 return(0);
  900.         }
  901.         else if (stat(path, &sbuf))
  902.             return(errno == ENOENT ? 1 : 0);
  903.  
  904.         /* tricky little algorithm for backward compatibility */
  905.         for (trv = start;;) {
  906.             if (!*trv)
  907.                 return(0);
  908.             if (*trv == 'z')
  909.                 *trv++ = 'a';
  910.             else {
  911.                 if (isdigit(*trv))
  912.                     *trv = 'a';
  913.                 else
  914.                     ++*trv;
  915.                 break;
  916.             }
  917.         }
  918.     }
  919.     /*NOTREACHED*/
  920. }
  921.  
  922. #endif
  923.